package day021.homework.p6;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        while (true) {
            System.out.println("等待客户端连接~~");
            Socket socket = serverSocket.accept();
            System.out.println("客户端连接成功：" + socket);

            InputStream netIn = socket.getInputStream();
            int len;
            byte[] buf = new byte[1024];
            while ((len = netIn.read(buf)) != -1) {
                System.out.println(new String(buf, 0, len));
            }

            OutputStream netOut = socket.getOutputStream();
            netOut.write("不约，没空，我要学习Java，等我毕业找到工作，再约！！".getBytes());
            socket.shutdownOutput();

            socket.close();
        }
    }
}
